package top.healthylife.gateway.platform.br.api.api.basicdata;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.br.api.utils.GetmeltingTokenUtils;
import top.healthylife.gateway.platform.br.dto.basicdata.mediaaccessindex.req.BrReqMediaAccessindexDto;
import top.healthylife.gateway.platform.br.dto.basicdata.mediaaccessindex.res.BrResMediaAccessindexDto;
import top.healthylife.gateway.platform.br.dto.basicdata.mediaaccessindex.res.MediaC;
import top.healthylife.gateway.platform.br.dto.basicdata.mediaaccessindex.res.MediaInfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cl
 * @version 1.0
 * @date 2020/4/22 17:13
 */
@Slf4j
@Service
@ApiServiceExplain( docUrl="https://account-self.100credit.com/", price = 20, apiName = "媒体阅览指数", info = "媒体阅览评估产品查询用户媒体阅览行为，是对阅览等级、类别等维度的统计评估（自然月）")
public class MediaAccessindexApi implements ParentFace<BrReqMediaAccessindexDto, BrResMediaAccessindexDto> {
    /**
     * 接口唯一标识
     */
    static final String APIIDENTIFICATION = "Media_c";

    /**
     * 接口版本号
     */
    static final String VESION = "2.0";

    @Override
    public BrResMediaAccessindexDto execute(BrReqMediaAccessindexDto brDemoApi) {
        //访问百融获取数据
        String apiData = GetmeltingTokenUtils.getApiData(APIIDENTIFICATION, VESION
                , BeanUtil.beanToMap(brDemoApi), GetmeltingTokenUtils.systemConfigInfo(brDemoApi));
        JSONObject jsonObject = JSONObject.parseObject(apiData);
        JSONObject mediaCJson = jsonObject.getJSONObject("Media_c");
        BrResMediaAccessindexDto brResMediaAccessindexDto = JSONObject.parseObject(apiData, BrResMediaAccessindexDto.class);
        JSONObject month3 = mediaCJson.getJSONObject("month3");
        List<MediaInfo> visitdays3 = month3.keySet().stream().map(key -> new MediaInfo().setCategoryName(key).setVisitdays(month3.getJSONObject(key).get("visitdays").toString())).collect(Collectors.toList());
        JSONObject month12 = mediaCJson.getJSONObject("month12");
        List<MediaInfo> visitdays12 = month12.keySet().stream().map(key -> new MediaInfo().setCategoryName(key).setVisitdays(month3.getJSONObject(key).get("visitdays").toString())).collect(Collectors.toList());
        MediaC mediaC = brResMediaAccessindexDto.getMediaC();
        mediaC.setMonth3(visitdays3).setMonth12(visitdays12);
        brResMediaAccessindexDto.setCls(BrResMediaAccessindexDto.class.getName());
        return brResMediaAccessindexDto;
    }
}
